using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CARMSBusiness;
using CARMSBusiness.InterFace;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;

namespace CARMS.AdminTemplate
{
    [Function("Admin.TemplateList")]
    public partial class TemplateDetail : PageBase
    {
        private int CurrentTemplateID
        {
            get
            {
                return GetIntByParameter("TemplateID");
            }
        }




        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDefault();
                LoadDetail();
            }
        }

        private void BindDefault()
        {
            ControlHelper.BindPublicItemsByTypeID(chkEventPermission, DictionaryCategoryConst.ButtonPermissionID, EmptyType.None);
           ControlHelper.BindReportCategoryList(chkReportCategoryList,EmptyType.None);
        }

        private void LoadDetail()
        {
            if (CurrentTemplateID > 0)
            {
                var entity = new TemplateEntity(CurrentTemplateID);
                if (!entity.IsNew)
                {
                    txtTemplateName.Text = entity.TemplateName;
                    txtContent.Text = entity.TemplateContent;
                    txtTemplateSubject.Text = entity.TemplateSubject;
                    txtRemark.Text = entity.Remark;


                    foreach (ListItem item in chkEventPermission.Items)
                    {
                        if (item.Value == DictionaryCategoryConst.ButtonPermission.Add.ToString() && item.Selected)
                        {
                            entity.Added = true;
                        }

                        if (item.Value == DictionaryCategoryConst.ButtonPermission.Change.ToString() && item.Selected)
                        {
                            entity.Changes = true;
                        }

                        if (item.Value == DictionaryCategoryConst.ButtonPermission.Confirm.ToString() && item.Selected)
                        {
                            entity.ToConfirm = true;
                        }

                        if (item.Value == DictionaryCategoryConst.ButtonPermission.Remove.ToString() && item.Selected)
                        {
                            entity.Remove = true;
                        }

                        if (item.Value == DictionaryCategoryConst.ButtonPermission.Export.ToString() && item.Selected)
                        {
                            entity.Export = true;
                        }

                        if (item.Value == DictionaryCategoryConst.ButtonPermission.Print.ToString() && item.Selected)
                        {
                            entity.Printing = true;
                        } 

                    }

                    foreach (var templateToReportCategory in entity.TemplateToReportCategory)
                    {
                        foreach (ListItem item in chkReportCategoryList.Items)
                        {
                            if (item.Value == templateToReportCategory.ReportCategoryId.ToString())
                            {
                                item.Selected = true;
                                break;
                            }
                        }
                    }
                }
            }
        }

   

        protected void btnSaveDetail_Click(object sender, EventArgs e)
        {
            var entity = new TemplateEntity(CurrentTemplateID)
            {
                TemplateName = txtTemplateName.Text.Trim(),
                TemplateContent = txtContent.Text.Trim(),
                TemplateSubject = txtTemplateSubject.Text.Trim(),
                Remark = txtRemark.Text.Trim()
            };

            foreach (ListItem item in chkEventPermission.Items)
            {
                if (item.Value == DictionaryCategoryConst.ButtonPermission.Add.ToString() && entity.Added)
                {
                    item.Selected = true;
                }
                if (item.Value == DictionaryCategoryConst.ButtonPermission.Change.ToString() && entity.Changes)
                {
                    item.Selected = true;
                }
                if (item.Value == DictionaryCategoryConst.ButtonPermission.Confirm.ToString() && entity.ToConfirm)
                {
                    item.Selected = true;
                }
                if (item.Value == DictionaryCategoryConst.ButtonPermission.Remove.ToString() && entity.Remove)
                {
                    item.Selected = true;
                }
                if (item.Value == DictionaryCategoryConst.ButtonPermission.Export.ToString() && entity.Export)
                {
                    item.Selected = true;
                }
                if (item.Value == DictionaryCategoryConst.ButtonPermission.Print.ToString() && entity.Printing)
                {
                    item.Selected = true;
                }
            }
            if (entity.Save())
            {
                if (entity.Save())
                {
                    var collection = new TemplateToReportCategoryCollection();
                    collection.DeleteMulti(TemplateToReportCategoryFields.TemplateId == entity.TemplateId);

                    foreach (ListItem item in chkReportCategoryList.Items)
                    {
                        if (item.Selected)
                        {
                            var templateToReportCategory = new TemplateToReportCategoryEntity
                                                               {
                                                                   TemplateId = entity.TemplateId,
                                                                   ReportCategoryId = int.Parse(item.Value)
                                                               };
                            templateToReportCategory.Save();
                        }
                    }
                }
               RediretResolveUrl(SiteUrls.AdminTemplate.TemplateList);
            }
            else
            {
                Alert(GenerateAlert.SaveFailed);
            }
        }


        protected void btnCancel_Click(object sender, EventArgs e)
        {
            RediretResolveUrl(SiteUrls.AdminTemplate.TemplateList);
        }

    }
}
